//
//  ZLLBarView.m
//  test
//
//  Created by Vincent on 2017/4/7.
//  Copyright © 2017年 Vincent. All rights reserved.
//

#import "ZLLBarView.h"





@implementation ZLLBarData

+(id)barDataWithXDestription:(NSAttributedString *)xBarDes yValue:(double)yval barColor:(UIColor *)barCol yBarDescription:(NSAttributedString *)ybarDes{
    
    ZLLBarData *data = [ZLLBarData new];
    data.xBarDes = xBarDes;
    data.yVal = yval;
    data.barColor = barCol;
    data.yBarDes = ybarDes;
    
    return data;
    
}

-(NSString *)description{
    
    return [NSString stringWithFormat:@"%@--->yValue:%.2f",[super description],self.yVal];
}

@end




@interface ZLLBarView ()



@end

@implementation ZLLBarView



-(UILabel *)titleLab{
    if(_titleLab==nil){
        _titleLab = [UILabel new];
        _titleLab.frame = CGRectMake(0,0, self.bounds.size.width, 20);
        _titleLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLab];
    }
    
    return _titleLab;
}

-(UIEdgeInsets)barEdge{
    if(UIEdgeInsetsEqualToEdgeInsets(_barEdge, UIEdgeInsetsZero)){
        _barEdge = UIEdgeInsetsMake(20, 40, 20, 0);
    }
    return _barEdge;
}

-(double)barWidth{
    
    if(_barWidth<=0){
        _barWidth = 30;
    }
    return _barWidth;
}

-(double)barSpace{
    if(_barSpace==0){
        _barSpace = (self.bounds.size.width-self.barEdge.left-self.barEdge.right-self.startOff-self.dataArr.count*self.barWidth)/(self.dataArr.count==0 ? 1 : self.dataArr.count);
    }
    return _barSpace;
}

-(UIColor *)dashColor{
    if(_dashColor==nil){
        _dashColor = [UIColor grayColor];
    }
    return _dashColor;
}

-(UIColor *)axisColor{
    if(_axisColor==nil){
        _axisColor = [UIColor blackColor];
    }
    return _axisColor;
}

-(double)yMax{
    if(_yMax<=0){
        _yMax = 1.0;
    }
    return _yMax;
}

-(NSNumberFormatter *)yFormatter{
    
    if(_yFormatter==nil){
        _yFormatter = [NSNumberFormatter new];
        _yFormatter.numberStyle = NSNumberFormatterDecimalStyle;
    }
    return _yFormatter;
}

-(int)ySection{
    if(_ySection<=0){
        _ySection = 5;
    }
    return _ySection;
}


-(NSDictionary *)yDrawOptions{
    NSDictionary *opt = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor blackColor]};
    
    return opt;
}


-(void)strokeBarView{
    
    [self setNeedsDisplay];
    
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGSize tmSize = [self.yUnit boundingRectWithSize:CGSizeMake(self.barEdge.left, 20) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    [self.yUnit drawInRect:CGRectMake((self.barEdge.left-tmSize.width),0, self.barEdge.left,tmSize.height)];
    
    //x、y坐标
    CGContextMoveToPoint(ctx, self.barEdge.left, 0);
    CGContextAddLineToPoint(ctx, self.barEdge.left, self.bounds.size.height-self.barEdge.bottom);
    CGContextAddLineToPoint(ctx, self.bounds.size.width-self.barEdge.right, self.bounds.size.height-self.barEdge.bottom);
    
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    CGContextSetLineWidth(ctx, 1);
    CGContextSetStrokeColorWithColor(ctx, self.axisColor.CGColor);
    CGContextStrokePath(ctx);
    
    
    double chartH = self.bounds.size.height-self.barEdge.top-self.barEdge.bottom;
    double sectH = chartH/self.ySection;
    
    //绘制y轴刻度虚线和描述文字
    for(int i=0;i<self.ySection+1;i++){
        CGContextMoveToPoint(ctx, self.barEdge.left, self.barEdge.top+sectH*i);
        CGContextAddLineToPoint(ctx, self.bounds.size.width-self.barEdge.right, self.barEdge.top+sectH*i);
        
        NSString *yStr = [self.yFormatter stringFromNumber:[NSNumber numberWithDouble:self.yMax-self.yMax/self.ySection*i]];
        NSAttributedString *att = [[NSAttributedString alloc] initWithString:yStr attributes:self.yDrawOptions];
        tmSize = [att boundingRectWithSize:CGSizeMake(self.barEdge.left, sectH-10) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        
        [att drawInRect:CGRectMake(self.barEdge.left-tmSize.width-5, self.barEdge.top+sectH*i-tmSize.height/2, self.barEdge.left, tmSize.height)];
    }
    CGFloat dash[] = {5,5};
    CGContextSetLineDash(ctx, 0, dash, 2);
    CGContextSetStrokeColorWithColor(ctx, self.dashColor.CGColor);
    CGContextSetLineWidth(ctx, 0.4);
    CGContextStrokePath(ctx);
    
    //绘制bar
    for(int i=0;i<self.dataArr.count;i++){
        ZLLBarData *data = self.dataArr[i];
        
        double rate = data.yVal/self.yMax;
        
        double offX = self.barEdge.left+self.startOff+(self.barWidth+self.barSpace)*i;
        CGContextAddRect(ctx, CGRectMake(offX, self.barEdge.top+(1-rate)*chartH, self.barWidth, rate*chartH));
        CGContextSetFillColorWithColor(ctx,data.barColor.CGColor);
        CGContextFillPath(ctx);
        
        tmSize = [data.yBarDes boundingRectWithSize:CGSizeMake(self.barWidth, 20) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        
        [data.yBarDes drawInRect:CGRectMake(offX+(self.barWidth-tmSize.width)/2, self.barEdge.top+(1-rate)*chartH-tmSize.height, tmSize.width, tmSize.height)];
        
        
        tmSize = [data.xBarDes boundingRectWithSize:CGSizeMake(self.barWidth, 20) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        [data.xBarDes drawInRect:CGRectMake(offX+(self.barWidth-tmSize.width)/2, self.bounds.size.height-self.barEdge.bottom+(self.barEdge.bottom-tmSize.height)/2, tmSize.width, tmSize.height)];
    }
    
}


-(double)fitViewWidthByDataArray:(NSArray<ZLLBarData *> *)dataArr{
    
    double tmW = self.barEdge.left+self.startOff+(self.barWidth+self.barSpace)*dataArr.count+self.barEdge.right;
    
    return tmW;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
